package cur.tool.controller;

import cur.nature.framework.mvc.crud.CrudController;
import cur.tool.external.mockserver.dabase.JdbcUtil;
import cur.tool.external.mockserver.shareDocs.ShareDocsProcessor;
import cur.tool.external.mockserver.shareDocs.base.ProcessorContext;
import cur.tool.service.Admin_DictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

/**
 * Mock服务 Controller
 * 作者：Auto Generator By wujian
 * 生成日期：2023-07-09 16:14:19
 */
@Tag(name = "Mock服务 接口")
@RestController
@RequestMapping("tool/v1/api/MockServer")
public class Tool_MockServerController extends CrudController<Admin_DictService> {
    public Tool_MockServerController(Admin_DictService baseService) {
        super(baseService);
    }


    @Operation(summary = "相关对外接口测试")
    @PostMapping(path="ShareDocs/**")
    public void shareDocs(HttpServletRequest request, HttpServletResponse response) {
        (new ShareDocsProcessor()).executeXml(new ProcessorContext(request,response));
    }

    @Operation(summary = "上传文件接口")
    @PostMapping("/upload.do")
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) { return "文件为空，请选择一个文件上传。"; }
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            String pathstr = "./files/"+fileName;
            File file1 = new File(pathstr);
            if (!file1.exists()) {
                file1.getParentFile().mkdirs();
            }
            System.out.println("path: "+pathstr);
            Path path = Paths.get(pathstr);
            Files.write(path, file.getBytes());
            // 返回成功信息
            return "文件上传成功： " + fileName + ".";
        } catch (Exception e) {
            return "文件上传失败： " + e.getMessage();
        }
    }

    @Operation(summary = "测试数据库连接")
    @GetMapping("/healthcheck.do")
    public String healthcheck() {
        try {
            String sql = "SELECT distinct 1 from admin_sys_dict";
            List<Map<String, Object>> list = JdbcUtil.query(sql);
            System.out.println(list.size());
            return "ok";
        }catch (Exception e) {return "err:"+e.getMessage();}


    }

}

